package net.misp.beta.paintapp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CanvasView extends ImageView {

	Context mContext;
	Paint mPaint, mText;
	Path mPath;
	Float posx, posy;
	Bitmap mBitmap = null;
	boolean isPaint = true;
	boolean isNotSaved = false;
	String mString = null;
	AlertDialog.Builder dlg;
	int mSize = 20;

	public CanvasView(Context context) {
		super(context);
		mContext = context;
		prepare();
	}

	private void prepare() {
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStrokeWidth(6);

		mText = new Paint();
		mText.setColor(Color.BLACK);
		mText.setTextSize(20);

		setBackgroundColor(Color.TRANSPARENT);

	}

	public CanvasView(Context context, AttributeSet attr) {
		super(context, attr);
		mContext = context;
		prepare();
	}

	public CanvasView(Context context, AttributeSet attr, int i) {
		super(context, attr, i);
		mContext = context;
		prepare();
	}

	public void clear() {
		mPath = null;
		mBitmap = null;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {

		if (mBitmap != null) {
			canvas.drawBitmap(mBitmap, 0, 0, mPaint);
		}

		if (mPath != null) {
			mPaint.setAntiAlias(true);
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeCap(Paint.Cap.ROUND);
			mPaint.setStrokeJoin(Paint.Join.ROUND);
			canvas.drawPath(mPath, mPaint);
		} else if (mString != null) {
			// TODO 未整理
			mText.setAntiAlias(true);
			mText.setStyle(Paint.Style.STROKE);
			mText.setStrokeCap(Paint.Cap.ROUND);
			mText.setStrokeJoin(Paint.Join.ROUND);
			int textSize = (int) (mText.getTextSize());
			int maxWidth = (int) (canvas.getWidth() - posx);
			int lineBreakPoint;
			int currentIndex = 0;
			int linePointY = posy.intValue();
			String[] lines = mString.split(String.valueOf('\n'));

			for (String msg : lines) {
				lineBreakPoint = Integer.MAX_VALUE;
				currentIndex = 0;

				while (lineBreakPoint != 0) {
					String mesureString = msg.substring(currentIndex);
					lineBreakPoint = mText.breakText(mesureString, true,
							maxWidth, null);
					if (lineBreakPoint != 0) {
						String line = msg.substring(currentIndex, currentIndex
								+ lineBreakPoint);
						canvas.drawText(line, posx.intValue(), linePointY,
								mText);
						linePointY += textSize;
						currentIndex += lineBreakPoint;
					}
				}
			}
			mString = null;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		posx = event.getX();
		posy = event.getY();
		if (isPaint) { // 自由描画モード
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mPath = new Path();
				mPath.moveTo(posx, posy);
				break;
			case MotionEvent.ACTION_MOVE:
				mPath.lineTo(posx, posy);
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				mPath.lineTo(posx, posy);
				setDrawingCacheEnabled(true);
				mBitmap = Bitmap.createBitmap(getDrawingCache());
				setDrawingCacheEnabled(false);
				setNotSaved(true);
				invalidate();
				mPath = null;
				break;
			default:
				break;
			}
			return true;
		} else { // �?��モー�?
			if (event.getAction() != MotionEvent.ACTION_DOWN)
				return false;

			dlg = createDialog();
			dlg.show();
			return true;
		}
	}

	private AlertDialog.Builder createDialog() {
		AlertDialog.Builder res = new AlertDialog.Builder(mContext);
		LinearLayout layout = new LinearLayout(mContext);
		layout.setOrientation(LinearLayout.VERTICAL);
		final EditText edit = new EditText(mContext);
		layout.addView(edit);
		res.setView(layout);
		res.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mString = edit.getText().toString();

				setDrawingCacheEnabled(true);
				mBitmap = Bitmap.createBitmap(getDrawingCache());
				setDrawingCacheEnabled(false);
				setNotSaved(true);
				invalidate();
			}
		});
		return res;
	}

	public void setColor(int a, int r, int g, int b) {
		if (isPaint)
			mPaint.setARGB(a, r, g, b);
		else
			mText.setARGB(a, r, g, b);
	}

	public void setColor(int color) {
		if (isPaint)
			mPaint.setColor(color);
		else
			mText.setColor(color);
	}

	public Bitmap getBitmap() {
		return mBitmap;
	}

	public void switchMode(boolean bool) {
		isPaint = bool;
	}

	public void setWeight(int weight) {
		mPaint.setStrokeWidth(weight);

	}

	public void setSize(int size) {
		mSize = size;
	}

	public void setNotSaved(boolean bl) {
		isNotSaved = bl;
	}

	public boolean isNotSaved() {
		return isNotSaved;
	}

	// public void onDlgOk(View view) {
	// mString = ((EditText) dlg.findViewById(R.id.dlg_input)).getText()
	// .toString();
	// setDrawingCacheEnabled(true);
	// mBitmap = Bitmap.createBitmap(getDrawingCache());
	// setDrawingCacheEnabled(false);
	// setNotSaved(true);
	// invalidate();
	// }
}
